

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:gczn_hd/app/core/values/app_colors.dart';
import 'package:gczn_hd/app/core/values/app_values.dart';
import 'package:gczn_hd/app/core/widget/gczn_card.dart';
import 'package:gczn_hd/app/modules/ppm/create_report/controllers/create_report_controller.dart';
import 'package:get/get.dart';
import 'package:gczn_hd/app/core/values/text_styles.dart';


class BadDrawer extends StatelessWidget{

  final CreateReportController controller = Get.put(CreateReportController());

  final Size buttonSize = Size(173.w, 60.h);
  final Size drawerSize =  Size(915.w,96.h);


  @override
  Widget build(BuildContext context) {
    double statusBarHeight = MediaQuery.of(context).padding.top;

    return Drawer(
      backgroundColor: AppColors.gcznDrawerBackground,
      width: drawerSize.width,
      child: Obx(()=>Container(
        alignment: Alignment.topLeft,
        child: Column(
          children: [
            _buildHeader(statusBarHeight),
            Expanded(
              flex: 1,
              child: Container(
                padding: EdgeInsets.only(top: AppValues.size39.h),
                child: SingleChildScrollView(
                  child: Wrap(
                    spacing: AppValues.size20.w,
                    runSpacing: AppValues.size20.h,
                    children: controller.badList.map((item) => _buildCheckItem(item)).toList(),
                  ),
                ),
              ),
            )
          ],
        ),
      )),
    );
  }

  Widget _buildCheckItem(Map<String, String> map){
    final double boxWidth = 430.w;

    return GcznCard(
      child: Container(
        padding: EdgeInsets.only(right: AppValues.size12.w),
        width: boxWidth,
        child: Row(
          children: [
            Expanded(
              flex: 1,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  Checkbox(
                    value: map['checked'] == "1",
                    onChanged: (value){
                      controller.toggleChecked(controller.badList.indexOf(map));
                    },
                    activeColor: AppColors.gcznPrimaryBlueColor,
                    checkColor: AppColors.colorWhite,
                  ),
                  Text(map['name']!, style: textStyle,),
                ],
              ),
            ),
            _buildCus(map)

          ],
        )
      ),
    );
  }

  Widget _buildCus(Map<String, String> map){
    final Size boxSize = Size(80.w, 48.h);

    return  Row(
    children: [
        _buildButton(type: 2, onPressed: (){
          controller.setNum(controller.badList.indexOf(map), false);
        }),
        Container(
          width: boxSize.width,
          height: boxSize.height,
          decoration: const BoxDecoration(
            border: Border(
                top: BorderSide(color: AppColors.counterBtn, width: 1),
                bottom: BorderSide(color: AppColors.counterBtn, width: 1)
            ),
          ),
          child: Center(
            child: Text(map['value']!, style: textStyle,),
          ),
        ),
        _buildButton(onPressed: (){
          controller.setNum(controller.badList.indexOf(map), true);
        })

      ],
    );

  }


  Widget _buildButton({int type = 1,  Function? onPressed}){
    final Size btnSize = Size(50.w, 50.h);

    return Container(
      width: btnSize.width,
      height:btnSize.height,
      decoration: BoxDecoration(
        color: AppColors.counterBtn,
        borderRadius: type == 1 ?
              BorderRadius.only(topRight: Radius.circular(AppValues.margin_12.r), bottomRight: Radius.circular(AppValues.margin_12.r))
            : BorderRadius.only(topLeft: Radius.circular(AppValues.margin_12.r), bottomLeft: Radius.circular(AppValues.margin_12.r))
      ),
      child:  IconButton(
        icon: Icon(type == 1 ? Icons.add : Icons.remove, color: Colors.white, size: AppValues.iconSize_18.sp,),
        onPressed: (){
          onPressed!();
        },
      ),
    );
  }



  Widget _buildHeader(double height){
    final Size btnSize =   Size(173.w, 60.h);

    return Container(
      width: drawerSize.width,
      height: (drawerSize.height+height),
      decoration: const BoxDecoration(
        gradient: LinearGradient(
          begin: Alignment.bottomCenter,
          end: Alignment.topCenter,
          colors: [
            Color.fromRGBO(18, 33, 47, 1),
            Color.fromRGBO(11, 23, 33, 1),
          ],
        ),
      ),
      child: Padding(
        padding: EdgeInsets.only(left: AppValues.size39.w, right: AppValues.size39.w, top: height.h),
        child: Row(
          children: [
            Expanded( flex: 1,
              child: Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Text('选择不良品项', style: whiteTextStyle.copyWith(fontSize: AppValues.extraLargeMargin.sp),),
                  SizedBox(width: AppValues.size52.w,),
                  Text("已选3项", style: whiteTextStyle.copyWith(color: AppColors.gcznPrimaryBlueColor),)
                ],
              ),
            ),
            Row(
              children: [
                SizedBox(width: btnSize.width, height: btnSize.height, child: ElevatedButton(
                  onPressed: () {
                    Get.back();
                  },
                  style: buttonStyle() ,
                  child: Text('取消', style: submitButtonText,),
                ))
                ,
                SizedBox(width: btnSize.width, height: btnSize.height,child: ElevatedButton(
                  onPressed: () {
                    // TODO
                  },
                  style: buttonStyle(idx: 2),
                  child: Text('确定', style: submitButtonText,),
                ),)
              ],
            )
          ],
        ),
      ),
    );

  }

  ButtonStyle buttonStyle({int idx = 1}){

    return idx == 1 ? ElevatedButton.styleFrom(
        backgroundColor: AppColors.gcznPrimaryColorCan,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(AppValues.margin_12.r),
            bottomLeft: Radius.circular(AppValues.margin_12.r),
          ),
        ),
        // fixedSize: btnSize
    ) : ElevatedButton.styleFrom(
        backgroundColor: AppColors.gcznPrimaryBlueColorBtn,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.only(
            topRight: Radius.circular(AppValues.margin_12.r),
            bottomRight: Radius.circular(AppValues.margin_12.r),
          ),
        ),
        // fixedSize: btnSize
    );

  }



}


